package com.eightywork.life.fragment;

import java.util.ArrayList;
import java.util.List;

import com.eightywork.life.R;
import com.eightywork.life.activity.GroupActivity;
import com.eightywork.life.adapter.GroupListAdapter;
import com.eightywork.life.model.Mtag;
import com.eightywork.life.service.CacheService;
import com.eightywork.life.service.SpaceService;
import com.eightywork.life.util.MyHttpException;
import com.eightywork.life.widget.ActionsContentView;
import com.eightywork.life.widget.RTPullListView;
import com.eightywork.life.widget.RTPullListView.OnRefreshListener;
import com.umeng.analytics.MobclickAgent;

import android.app.Activity;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;

public class GroupFragment extends BaseFragment {
	public static final String TAG = GroupFragment.class.getName();
	private ActionsContentView viewActionsContentView;
	private Activity context;
	private RTPullListView listView;
	private GroupListAdapter adapter;
	private List<Mtag> list = new ArrayList<Mtag>();
	private AsyncTask task;

	public GroupFragment(Activity context,
			ActionsContentView viewActionsContentView) {
		this.context = context;
		this.viewActionsContentView = viewActionsContentView;
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		return inflater.inflate(R.layout.fragment_group, container, false);
	}
	
	@Override
	public void onResume() {
		super.onResume();
	    MobclickAgent.onResume(getActivity());
	}
	
	@Override
	public void onPause() {
	    super.onPause();
	    MobclickAgent.onPause(getActivity());
	}
	
	@Override
	public void onDestroy() {
		if (task != null) {
			task.cancel(true);
		}
		super.onDestroy();
	}


	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		init();
		super.onActivityCreated(savedInstanceState);
	}

	private void init() {
		initHeader("圈子", viewActionsContentView);
		adapter = new GroupListAdapter(context, list);
		listView = (RTPullListView) getView().findViewById(R.id.listView1);
		listView.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long position) {
				Intent intent = new Intent(context, GroupActivity.class);
				intent.putExtra("id", list.get((int)position).getTagid());
				intent.putExtra("name", list.get((int)position).getTagname());
				intent.putExtra("ismember",  list.get((int)position).getIsmember());
				context.startActivity(intent);
			}
		});
		listView.setAdapter(adapter);
		listView.setonRefreshListener(new OnRefreshListener() {
			@Override
			public void onRefresh() {
				getList();
			}
		});
		listView.goLoading();
	}

	public void getList() {
		task = new GroupTask().execute();
	}

	class GroupTask extends AsyncTask<Void, Void, String> {
		private List<Mtag> aList;
		@Override
		protected void onPreExecute() {
			super.onPreExecute();
		}

		@Override
		protected String doInBackground(Void... params) {
			try {
				aList = SpaceService.mtag(CacheService.getSpace()
						.getXiaoquid());
				
			} catch (MyHttpException e) {
				return e.getErrorMessage();
			}
			return null;
		}

		@Override
		protected void onPostExecute(String result) {
			if (result == null) {
				if(aList != null){
					list.clear();
					list.addAll(aList);
				}	
				listView.onRefreshComplete();
				adapter.notifyDataSetChanged();
			} else {
				getBaseActivity().showMessage(result);
			}
			super.onPostExecute(result);
		}
	}
}
